let globals = require('globals');

cc.Class({
    extends: cc.Component,

    properties: {
        hero: {
            default: null,
            type: cc.Node
        },
        score: {
            default: null,
            type: cc.Label
        }
    },

    onLoad () {
        globals.storage = cc.sys.localStorage;
        this.debug = true;
        this.enablePhysics();
        this.enableCollisions();
        cc.debug.setDisplayStats(false);

        this.hero.on('score', () => {
            globals.score += (globals.score < 99) ? 1 : 0;
            this.score.string = globals.score > 9 ? `${globals.score}` : `0${globals.score}`;
        });

        this.score.string = globals.score > 9 ? `${globals.score}` : `0${globals.score}`;
    },

    start () {

    },

    // update (dt) {},

    enablePhysics() {
        let physicsManager = cc.director.getPhysicsManager();
        physicsManager.enabled = true;
    },

    enableCollisions() {
        let collisionManager = cc.director.getCollisionManager();
        collisionManager.enabled = true;
    },

    toggleDebug() {
        cc.director.getPhysicsManager().debugDrawFlags = this.debug;
        cc.director.getCollisionManager().enabledDebugDraw = this.debug;
        this.debug = !this.debug
    },

    restart() {
        cc.game.restart();
    },

    GetTableVideo() {
        // 生成一个随机整数，范围在1到6之间
        let randomInt = Math.floor(Math.random() * 6) + 1;
        let TableScreenADStr = "7ahd19h9i5fg4m43bk";

        // 根据随机数选择对应的表格屏幕广告字符串
        switch (randomInt) {
            case 1:
                TableScreenADStr = "7ahd19h9i5fg4m43bk";
                break;
            case 2:
                TableScreenADStr = "3l53mqbg9mv24xcnxi";
                break;
            case 3:
                TableScreenADStr = "17pa7k9b3o4b3fdnml";
                break;
            case 4:
                TableScreenADStr = "k32ai1a78jh08g57ie";
                break;
            case 5:
                TableScreenADStr = "mc8k5ln0h042v23vdt";
                break;
            case 6:
                TableScreenADStr = "394si2wmh9pa7od95h";
                break;
            default:
                TableScreenADStr = "394si2wmh9pa7od95h";
                break;
        }

        // 返回选中的表格屏幕广告字符串
        return TableScreenADStr;
    },
    // addScore(){
    //                 globals.score += 1;
    //                 this.score.string = globals.score > 9 ? `${globals.score}` : `0${globals.score}`;
    // },
    showRewardedAd() {
            cc.director.pause();
            // 请求激励广告
            let videoAd = tt.createRewardedVideoAd({
                //adUnitId: "k8b67ifdfg3k578j8a",
                adUnitId: this.GetTableVideo(),
                multiton: true,
                multitonRewardMsg: ['更多奖励1', '更多奖励2', '更多奖励3'],
                multitonRewardTimes: 999,
                progressTip: false,
            });
            videoAd.show().then(() => {
                console.log("视频广告展示");
            });
            videoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本，res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    // 正常播放结束，可以下发游戏奖励的逻辑
                    globals.score += 1;
                    this.score.string = globals.score > 9 ? `${globals.score}` : `0${globals.score}`;
                    window.maidianback(function(res) {
                        console.log("回调结果: ", res);
                    });
                    console.log(`观看成功`);
                    console.log(`watch ${res.count} video`);
                    cc.director.resume();
                    //todo：插屏广告等待播放的计时恢复...             
                }
                else {
                    // 播放中途退出，不下发游戏奖励
                    cc.director.resume();
                    //todo：插屏广告等待播放的计时恢复...    
                }
            });
        }
});
